/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoBase;
import model.dao.DisciplinaJpaController;
import model.dao.ProfessorJpaController;
import model.dao.TurmaJpaController;
import model.dao.exceptions.NonexistentEntityException;
import model.pojo.Aluno;
import model.pojo.Disciplina;
import model.pojo.Professor;
import model.pojo.Turma;

/**
 *
 * @author leonidia
 */
public class EditarTurma extends javax.swing.JFrame {

    Integer opcaoDisciplina;
    Integer opcaoProfessor;
    ArrayList<String> listaDisciplina;
    ArrayList<String> listaProfessor;
    TurmaJpaController turmaJpaController = null;
    List<Disciplina> listaD;
    List<Professor> listaP;
    List<Turma> listaT;
    List<Aluno> listaA;
    List<Aluno> alunos = new LinkedList<>();
    DefaultTableModel tabelaGeral;
    Integer tamanhoTabela = 0;
    DaoBase daoBase = null;
    Turma turma;
    Disciplina disciplina;
    Professor professor;

    /**
     * Creates new form EditarTurma
     */
    public EditarTurma(DaoBase daoBase) {
        initComponents();
        this.daoBase = daoBase;
        turmaJpaController = new TurmaJpaController(null, daoBase.returnFactory());
        AtualizaDisciplina();
        AtualizaProfessor();
        atualizarTabela();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    public void AtualizaDisciplina() {
        DisciplinaJpaController disciplinaJpaController = new DisciplinaJpaController(null, daoBase.returnFactory());
        listaDisciplina = new ArrayList<String>();
        listaD = disciplinaJpaController.findDisciplinaEntities();

        for (Disciplina item : listaD) {
            listaDisciplina.add(item.getNome());
        }
        DefaultComboBoxModel defaultDisciplina = new DefaultComboBoxModel(listaDisciplina.toArray());
        ListaDisciplina.setModel(defaultDisciplina);
    }

    public void AtualizaProfessor() {
        ProfessorJpaController professorJpaController = new ProfessorJpaController(null, daoBase.returnFactory());
        listaProfessor = new ArrayList<String>();
        listaP = professorJpaController.findProfessorEntities();

        for (Professor item : listaP) {
            listaProfessor.add(item.getNome());
        }
        DefaultComboBoxModel defaultProfessor = new DefaultComboBoxModel(listaProfessor.toArray());
        ListaProfessor.setModel(defaultProfessor);
    }

    public void atualizarTabela() {
        DefaultTableModel model = (DefaultTableModel) TabelaTurma.getModel();
        TurmaJpaController turmaJpaController = new TurmaJpaController(null, daoBase.returnFactory());
        listaT = turmaJpaController.findTurmaEntities();
        for (Turma item : listaT) {
            model.addRow(new Object[]{item.getAno(), item.getPeriodo(), item.getHorario(), item.getLocal(), item.getNumeroDeVagas(),item.getDisciplina().getNome()});
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        Ano = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        Vagas = new javax.swing.JTextField();
        Local = new javax.swing.JTextField();
        Periodo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        Horario = new javax.swing.JFormattedTextField();
        ListaDisciplina = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        ListaProfessor = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        Editar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        TabelaTurma = new javax.swing.JTable();

        jLabel7.setText("Local");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Editar Turma");

        jLabel6.setText("Horário");

        jLabel5.setText("Período ");

        jLabel4.setText("Ano");

        try {
            Ano.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        Ano.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnoActionPerformed(evt);
            }
        });

        jLabel8.setText("Vagas");

        jLabel9.setText("(xx:xx)");

        Vagas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VagasActionPerformed(evt);
            }
        });

        jLabel3.setText("Professores");

        try {
            Horario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        ListaDisciplina.setToolTipText("");
        ListaDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListaDisciplinaActionPerformed(evt);
            }
        });

        jLabel2.setText("Disciplinas");

        ListaProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListaProfessorActionPerformed(evt);
            }
        });

        jLabel10.setText("Local");

        Editar.setText("Editar");
        Editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditarActionPerformed(evt);
            }
        });

        TabelaTurma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ano", "Periodo", "Horario", "Local", "Vagas", "Disciplina"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TabelaTurma.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TabelaTurmaMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(TabelaTurma);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(168, 168, 168)
                .addComponent(jLabel1))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2)
                .addGap(258, 258, 258)
                .addComponent(jLabel4)
                .addGap(1, 1, 1)
                .addComponent(Ano, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jLabel5)
                .addGap(1, 1, 1)
                .addComponent(Periodo, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ListaDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(36, 36, 36)
                .addComponent(jLabel6)
                .addGap(5, 5, 5)
                .addComponent(Horario, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jLabel9)
                .addGap(4, 4, 4)
                .addComponent(jLabel8)
                .addGap(11, 11, 11)
                .addComponent(Vagas, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(ListaProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jLabel10)
                .addGap(6, 6, 6)
                .addComponent(Local, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(175, 175, 175)
                .addComponent(Editar))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel2))
                    .addComponent(jLabel4)
                    .addComponent(Ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(Periodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ListaDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(Horario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8)
                            .addComponent(Vagas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(ListaProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel10))
                    .addComponent(Local, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(Editar))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_AnoActionPerformed

    private void VagasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VagasActionPerformed
        // TODO add your handling code here:
        tamanhoTabela = Integer.parseInt(Vagas.getText());
    }//GEN-LAST:event_VagasActionPerformed

    private void ListaDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListaDisciplinaActionPerformed
        // TODO add your handling code here:
        opcaoDisciplina = ListaDisciplina.getSelectedIndex();
    }//GEN-LAST:event_ListaDisciplinaActionPerformed

    private void ListaProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListaProfessorActionPerformed
        // TODO add your handling code here:
        opcaoProfessor = ListaProfessor.getSelectedIndex();
    }//GEN-LAST:event_ListaProfessorActionPerformed

    private void TabelaTurmaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TabelaTurmaMouseClicked
        // TODO add your handling code here:
        turma = listaT.get(TabelaTurma.getSelectedRow());
        Ano.setText(Integer.toString(turma.getAno()));
        Periodo.setText(Integer.toString(turma.getPeriodo()));
        Horario.setText(turma.getHorario());
        Vagas.setText(Integer.toString(turma.getNumeroDeVagas()));
        Local.setText(turma.getLocal());
        professor = turma.getProfessor();
        disciplina = turma.getDisciplina();
    }//GEN-LAST:event_TabelaTurmaMouseClicked

    private void EditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditarActionPerformed
        try {
            // TODO add your handling code here:
            professor = listaP.get(ListaProfessor.getSelectedIndex());
            disciplina = listaD.get(ListaDisciplina.getSelectedIndex());
            turma.setAno(Integer.parseInt(Ano.getText()));
            turma.setPeriodo(Integer.parseInt(Periodo.getText()));
            turma.setLocal(Local.getText());
            turma.setHorario(Horario.getText());
            turma.setNumeroDeVagas(Integer.parseInt(Vagas.getText()));
            turma.setProfessor(professor);
            turma.setDisciplina(disciplina);
            turmaJpaController.edit(turma);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(EditarTurma.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(EditarTurma.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            dispose();
        }
    }//GEN-LAST:event_EditarActionPerformed

    /**
     * @param args the command line arguments
     */
    public void run() {
        setVisible(true);
    }

    public void stop() {
        setVisible(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField Ano;
    private javax.swing.JButton Editar;
    private javax.swing.JFormattedTextField Horario;
    private javax.swing.JComboBox ListaDisciplina;
    private javax.swing.JComboBox ListaProfessor;
    private javax.swing.JTextField Local;
    private javax.swing.JTextField Periodo;
    private javax.swing.JTable TabelaTurma;
    private javax.swing.JTextField Vagas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
